$j(document).ready(function () {
	/*
	$j('#quantityOnHand').bind("change",adjustQuantityOnHand).bind("keyup",adjustQuantityOnHand);
	$j('input[name="quantityOnHandCheck"]').bind("change",switchAdjustBtn).bind("click",switchAdjustBtn);
	//绑定tab选择事件
	$j('#detail').bind('tabsselect', function(event, ui) {
		switcBtnVisible();		
	});
	loadAuditList();
	*/
	
});
var allocatedQuantity; //已分配库存
var quantityOnHand ;//现有库存
//调整库存
function adjustInventory(){
    var postData="";
    postData += $j('#adjustInventoryForm :input').serialize();
    postData += "&comment="+$j('#comment').val();
    var url=$j('#adjustInventoryForm').attr('action')+"?doAction=adjustInventory";
    $j.post(url,postData,adjustInventoryCallback,"json");
    dlgadjustInventoryConfirmDialog_close();
}
//调整库存callback
function adjustInventoryCallback(data){
	quantityOnHand = data.quantityOnHand;
	$j('#comment').val("");
	disableIconBtn("adjustInventory");
	jAlert(__FMT.inventoryDetail_adjustSuccess);
	loadAuditList();
}
//调整现有库存
function adjustQuantityOnHand(){
	removeValidate($("quantityOnHand"));
	applyValidate($("quantityOnHand"),"integer,intRange=[0-100000]");
	if(validateInventory($j("#adjustInventoryForm")[0]) && $j('#quantityOnHand').val().trim()!=""){
		var nowQuantityOnHand = parseInt($j('#quantityOnHand').val().trim());
		$j('#availableQuantity').html(""+(nowQuantityOnHand-allocatedQuantity));
		switchAdjustBtn();
	}
	
}
//改变按钮的可见
function switcBtnVisible(){
	//要注意判断的应该是将要变成select的tab
	if(getSelectedTabId()==1){
		$j('#adjustInventory').show();
		$j('#save').hide();
	}else if(getSelectedTabId()==0){
		$j('#adjustInventory').hide();
		$j('#save').show();
	}
}
//改变按钮enable或者disable
function switchAdjustBtn(){
	var quantityOnHandCheck = "";
	$j('input[name="quantityOnHandCheck"]').each(function(i,o){
		if(o.checked){
			quantityOnHandCheck = o.value;
		}
	});
	if((quantityOnHandCheck==1 && quantityOnHand=="") | (quantityOnHandCheck==0 && quantityOnHand!="")  ){
		enableIconBtn("adjustInventory");
	}else{
		var nowQuantityOnHand = parseInt($j('#quantityOnHand').val().trim());
		if(nowQuantityOnHand != quantityOnHand){
			//数量有更改
			enableIconBtn("adjustInventory");
		}else{
			disableIconBtn("adjustInventory");
		}
	}
	
	
	
}
//打开调整库存理由dialog
function showAdjustInventoryConfirmDialog(){
	removeValidate($("quantityOnHand"));
	applyValidate($("quantityOnHand"),"required,integer,intRange=[0-100000]");
	if(validateInventory($j("#adjustInventoryForm")[0])){
		dlgadjustInventoryConfirmDialog_show();
	}	
}

//保存库存参数设定
function saveSetting(){
	var postData="";
    postData += $j('#inventoryForm :input').serialize();
    var url=$j('#inventoryForm').attr('action')+"?doAction=saveSetting";
    $j.post(url,postData,saveSettingCallback,"json");
}
//保存库存参数设定回调函数
function saveSettingCallback(){
	jAlert(__FMT.inventoryDetail_saveSettingSuccess);
}

//获得日志列表
function loadAuditList(){
	var url=__ctxPath+"/inventory/inventory/dialog.html?doAction=getAuditList&productSkuId="+$('productSkuId').value;
	$j("#auditList").load(url);  
}
String.prototype.trim= function(){  
   	// 用正则表达式将前后空格  
   	// 用空字符串替代。  
   	return this.replace(/(^\s*)|(\s*$)/g, "");  
}